#include"shared_command.hpp"


bool Makefifo()
{
    int n=mkfifo(FIFLNAME,0666);
    if(n<0)
    {
        cerr<<"server create FIFO failed,errno:"<<errno<<strerror(errno)<<endl;
        return false;
    }
    return true;
}

int main()
{
start:

    int fd = open(FIFLNAME,O_RDONLY);
    if(fd<0)
    {
        if(Makefifo())
            goto start;
        else
        {
            cerr<<"server open FIFO failed,errno:"<<errno<<strerror(errno)<<endl;
            return 1;
        }
    }

    while(true)
    {
        char message[Size];
        ssize_t n = read(fd,message,Size);
        if(n<0)
        {
            cerr<<"server read error!,errno:"<<errno<<strerror(errno)<<endl;
            break;
        }
        else if(n==0)
        {
            cout<<"client already close!"<<endl;
            break;
        }
        message[n]=0;
        cout<<"client#:"<<message<<endl;

    }

    close(fd);
    cout<<"server close FIFO succeed!"<<endl;
    cout<<"server exit!"<<endl;
}